Skip to content

Bugfix: Implement tolerant JSON decoding#1553

Merged
liudger merged 1 commit into
mainfrom
fall-back-to-latin-decode
Jul 5, 2026
Merged

Bugfix: Implement tolerant JSON decoding#1553
liudger merged 1 commit into
mainfrom
fall-back-to-latin-decode

Conversation

@liudger

@liudger liudger commented Jul 5, 2026

Copy link
Copy Markdown
Owner

This pull request improves the robustness of the BSB-LAN integration by making JSON response decoding tolerant to non-UTF-8 encodings, specifically handling Latin-1 encoded responses. It introduces a custom method for decoding JSON, updates error handling, and adds new tests to ensure correct behavior for both UTF-8 and Latin-1 encoded responses.

Response decoding improvements:

  • Added a static method _read_json to src/bsblan/_transport.py to decode JSON responses, first attempting UTF-8 and falling back to Latin-1 if necessary, addressing issues with BSB-LAN firmwares that serve Latin-1 encoded responses without a charset declaration. [1] [2]
  • Updated the request_with_retry method to use the new _read_json method, ensuring all responses are decoded using the tolerant logic.

Testing enhancements:

  • Added a new test file tests/test_response_encoding.py with tests verifying that Latin-1 and UTF-8 encoded responses are both decoded correctly.
  • Updated the edge case test in tests/test_bsblan_edge_cases.py to mock the new decoding logic and check for the updated error message when invalid JSON is encountered.

Copilot AI review requested due to automatic review settings July 5, 2026 18:23
@liudger liudger added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Jul 5, 2026
@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b3c2c31) to head (4476516).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1553   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        14           
  Lines         1325      1334    +9     
  Branches       142       142           
=========================================
+ Hits          1325      1334    +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud

sonarqubecloud Bot commented Jul 5, 2026

Copy link
Copy Markdown

@liudger liudger merged commit 54a5484 into main Jul 5, 2026
18 checks passed
@liudger liudger deleted the fall-back-to-latin-decode branch July 5, 2026 18:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the robustness of response handling in the BSBLAN client by making JSON decoding tolerant to devices that return Latin-1-encoded JSON without declaring a charset, and extends test coverage to validate the behavior.

Changes:

  • Added _Transport._read_json() to decode response bodies as UTF-8 with a Latin-1 fallback, then parse JSON.
  • Switched request_with_retry() to use _read_json() instead of aiohttp’s response.json().
  • Added encoding-focused tests and updated the invalid-JSON edge case to align with the new decoding path.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/bsblan/_transport.py Introduces tolerant JSON decoding and routes all request decoding through it.
tests/test_response_encoding.py Adds tests validating UTF-8 and Latin-1 response decoding.
tests/test_bsblan_edge_cases.py Updates the invalid JSON test to mock read() and assert the wrapped error message.

Comment thread src/bsblan/_transport.py
Comment on lines +167 to +171
try:
text = raw.decode("utf-8")
except UnicodeDecodeError:
text = raw.decode("latin-1")
return cast("dict[str, Any]", json.loads(text))
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 7, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants